//
//  RTCNetWorkingManager.m
//  YouinRTC
//
//  Created by 潘欣 on 2022/8/20.
//

#import "RTCNetWorkingManager.h"

@interface RTCNetWorkingManager ()

/** 网络管理类实例 */
@property (nonatomic, strong) AFHTTPSessionManager *httpSessionManager;


@end


@implementation RTCNetWorkingManager

/**
 网络管理类实例
 
 @return 网络管理类实例
 */
- (AFHTTPSessionManager *)httpSessionManager {
    
    if (nil == _httpSessionManager) {
        // 1.初始化：
        _httpSessionManager = [AFHTTPSessionManager manager];
//        _httpSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
//        _httpSessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        _httpSessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        _httpSessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        // 缓存策略
        _httpSessionManager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        //设置超时时间
        [_httpSessionManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _httpSessionManager.requestSerializer.timeoutInterval = 30.f;
        [_httpSessionManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
                
        // 3.设置允许接收返回数据类型：
        [_httpSessionManager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", @"application/x-javascript",@"image/png", nil]];
    }
    
    return _httpSessionManager;
}

/**
 创建一个单例的对象
 */
RYSingletonM(RTCNetWorkingManager)

- (void)requestPost:(NSString *)urlString params:(NSDictionary *_Nullable)params header:(NSDictionary * _Nullable)header success:(successBlock)success failure:(requestFailure)failure
{
    
    [self.httpSessionManager POST:urlString parameters:params headers:header progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            !success ? : success(responseObject);
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            !failure ? : failure(error);
        });
    }];
}

- (void)requestGet:(NSString *)urlString paramars:(NSDictionary *_Nullable)paramars header:(NSDictionary *_Nullable)header success:(successBlock)success failure:(requestFailure)failure
{
    
    [self.httpSessionManager GET:urlString parameters:paramars headers:header progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            !success ? : success(responseObject);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        
        dispatch_async(dispatch_get_main_queue(), ^{
            !failure ? : failure(error);
        });
    }];
}


- (void)requestUpLoadFile:(NSData *)data withUrl:(NSString *)urlString params:(NSDictionary *_Nullable)params header:(NSDictionary *_Nullable)header progress:(RYNSProgress)progress success:(successBlock)success failure:(requestFailure)failure
{
    // 接口参数
    NSMutableDictionary *paramsDictionary = [NSMutableDictionary dictionary];

    paramsDictionary[@"file"] = data;
    [paramsDictionary addEntriesFromDictionary:params];
    
    NSURLSessionDataTask *sessionDataTask = [self.httpSessionManager POST:urlString parameters:paramsDictionary headers:header constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
        /**
         上传方法参数
         
         1. data:要上传的[二进制数据]
         2. name:对应网站上处理文件的[字段"file"]
         3. fileName:要保存在服务器上的[文件名]
         4. mimeType:上传文件的[mimeType]
         */
        [formData appendPartWithFileData:data name:@"file" fileName:params[@"filename"] mimeType:@"image/png"];
    } progress:^(NSProgress *uploadProgress) {
        !progress ? : progress(uploadProgress);
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        !success ? : success(responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        !failure ? : failure(error);
    }];
    
    [sessionDataTask resume];
}
@end
